module RedmineIssueStatusBgcolor
  module CssColorPatch
    def self.included(base) # :nodoc:
      base.send(:include, InstanceMethods)
    end

    module InstanceMethods
      # calculates black or white font-color for a given background color
      def rti_contrast_css_color( _html_color )
        return "black" unless _html_color
        begin
          _rgb = _html_color.to_s.downcase.scan(/[0-9a-f]{2}/).map(&:hex)
          (0.213 * _rgb[0] + 0.715 * _rgb[1] + 0.072 * _rgb[2])/255 > 0.5 ? "orange" : "lightyellow"
        rescue
          "black"
        end
      end #def
      # lightens color cvalue
      def rti_lighten_css_color(hex_color, amount=0.6)
        return "#ffffff" unless hex_color
        hex_color = hex_color.to_s.gsub('#','')
        rgb = hex_color.scan(/../).map {|color| color.hex}
        rgb[0] = [(rgb[0].to_i + 255 * amount).round, 255].min
        rgb[1] = [(rgb[1].to_i + 255 * amount).round, 255].min
        rgb[2] = [(rgb[2].to_i + 255 * amount).round, 255].min
        "#%02x%02x%02x" % rgb
      end #def
    end
  end
end

unless ApplicationHelper.included_modules.include?(RedmineIssueStatusBgcolor::CssColorPatch)
    ApplicationHelper.send(:include, RedmineIssueStatusBgcolor::CssColorPatch)
end